

package com.hazelcast.internal.metrics.impl;

import com.hazelcast.internal.metrics.MetricDescriptor;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.hazelcast.internal.metrics.ProbeUnit.BYTES;
import static com.hazelcast.internal.metrics.ProbeUnit.COUNT;

/**
 * This class is used for generating the binary file to be used in non-Java clients
 * to validate metrics binary format compatibility.
 * <p>
 * Run this class once. Then move the created file to resources directory.
 * <p>
 * mv *binary src/test/resources/
 */
final class MetricsCompatibilityFileGenerator {

    private MetricsCompatibilityFileGenerator() {
    }

    public static void main(String[] args) throws IOException {
        OutputStream out = new FileOutputStream(createFileName());
        DataOutputStream outputStream = new DataOutputStream(out);
        byte[] blob = generateBlob();
        outputStream.write(blob);
        outputStream.close();
    }

    private static String createFileName() {
        return "metrics.compatibility.binary";
    }

    private static byte[] generateBlob() {
        DefaultMetricDescriptorSupplier supplier = new DefaultMetricDescriptorSupplier();
        MetricsCompressor compressor = new MetricsCompressor();

        MetricDescriptor metric1 = supplier.get().withPrefix("prefix").withMetric("deltaMetric1").withDiscriminator("ds", "dsName1").withUnit(COUNT);
        compressor.addLong(metric1, 42);

        MetricDescriptor metric2 = metric1.copy().withMetric("deltaMetric2");
        compressor.addDouble(metric2, -4.2);

        // use a 254 byte string to test long words in the dictionary
        String longPrefix = Stream.generate(() -> "a").limit(MetricsDictionary.MAX_WORD_LENGTH - 1).collect(Collectors.joining());
        MetricDescriptor metric3 = supplier.get().withPrefix(longPrefix).withMetric("longPrefixMetric").withUnit(BYTES);
        compressor.addLong(metric3, Integer.MAX_VALUE);

        return compressor.getBlobAndReset();
    }
}
